﻿using System;
using System.ComponentModel;

namespace HalconDotNet
{
	/// <summary>Represents an instance of a connection via a serial port.</summary>
	// Token: 0x0200005F RID: 95
	public class HSerial : HHandle
	{
		// Token: 0x0600176F RID: 5999 RVA: 0x0000883B File Offset: 0x00006A3B
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HSerial() : base(HHandleBase.UNDEF)
		{
		}

		// Token: 0x06001770 RID: 6000 RVA: 0x00096632 File Offset: 0x00094832
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HSerial(IntPtr handle) : base(handle)
		{
			this.AssertSemType();
		}

		// Token: 0x06001771 RID: 6001 RVA: 0x00096641 File Offset: 0x00094841
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HSerial(HHandle handle) : base(handle)
		{
			this.AssertSemType();
		}

		// Token: 0x06001772 RID: 6002 RVA: 0x00096650 File Offset: 0x00094850
		private void AssertSemType()
		{
			base.AssertSemType("serial");
		}

		// Token: 0x06001773 RID: 6003 RVA: 0x0009665D File Offset: 0x0009485D
		internal static int LoadNew(IntPtr proc, int parIndex, int err, out HSerial obj)
		{
			obj = new HSerial(HHandleBase.UNDEF);
			return obj.Load(proc, parIndex, err);
		}

		// Token: 0x06001774 RID: 6004 RVA: 0x00096678 File Offset: 0x00094878
		internal static int LoadNew(IntPtr proc, int parIndex, int err, out HSerial[] obj)
		{
			HTuple htuple;
			err = HTuple.LoadNew(proc, parIndex, err, out htuple);
			obj = new HSerial[htuple.Length];
			for (int i = 0; i < htuple.Length; i++)
			{
				obj[i] = new HSerial(HalconAPI.IsLegacyHandleMode() ? htuple[i].IP : htuple[i].H);
			}
			htuple.Dispose();
			return err;
		}

		/// <summary>
		///   Open a serial device.
		///   Modified instance represents: Serial interface handle.
		/// </summary>
		/// <param name="portName">Name of the serial port. Default: "COM1"</param>
		// Token: 0x06001775 RID: 6005 RVA: 0x000966E8 File Offset: 0x000948E8
		public HSerial(string portName)
		{
			IntPtr proc = HalconAPI.PreCall(314);
			HalconAPI.StoreS(proc, 0, portName);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Clear the buffer of a serial connection.
		///   Instance represents: Serial interface handle.
		/// </summary>
		/// <param name="channel">Buffer to be cleared. Default: "input"</param>
		// Token: 0x06001776 RID: 6006 RVA: 0x00096734 File Offset: 0x00094934
		public void ClearSerial(string channel)
		{
			IntPtr proc = HalconAPI.PreCall(307);
			base.Store(proc, 0);
			HalconAPI.StoreS(proc, 1, channel);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Write to a serial connection.
		///   Instance represents: Serial interface handle.
		/// </summary>
		/// <param name="data">Characters to write (as tuple of integers).</param>
		// Token: 0x06001777 RID: 6007 RVA: 0x00096770 File Offset: 0x00094970
		public void WriteSerial(HTuple data)
		{
			IntPtr proc = HalconAPI.PreCall(308);
			base.Store(proc, 0);
			HalconAPI.Store(proc, 1, data);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(data);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Write to a serial connection.
		///   Instance represents: Serial interface handle.
		/// </summary>
		/// <param name="data">Characters to write (as tuple of integers).</param>
		// Token: 0x06001778 RID: 6008 RVA: 0x000967B4 File Offset: 0x000949B4
		public void WriteSerial(int data)
		{
			IntPtr proc = HalconAPI.PreCall(308);
			base.Store(proc, 0);
			HalconAPI.StoreI(proc, 1, data);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Read from a serial device.
		///   Instance represents: Serial interface handle.
		/// </summary>
		/// <param name="numCharacters">Number of characters to read. Default: 1</param>
		/// <returns>Read characters (as tuple of integers).</returns>
		// Token: 0x06001779 RID: 6009 RVA: 0x000967F0 File Offset: 0x000949F0
		public HTuple ReadSerial(int numCharacters)
		{
			IntPtr proc = HalconAPI.PreCall(309);
			base.Store(proc, 0);
			HalconAPI.StoreI(proc, 1, numCharacters);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HTuple result;
			num = HTuple.LoadNew(proc, 0, HTupleType.INTEGER, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Get the parameters of a serial device.
		///   Instance represents: Serial interface handle.
		/// </summary>
		/// <param name="dataBits">Number of data bits of the serial interface.</param>
		/// <param name="flowControl">Type of flow control of the serial interface.</param>
		/// <param name="parity">Parity of the serial interface.</param>
		/// <param name="stopBits">Number of stop bits of the serial interface.</param>
		/// <param name="totalTimeOut">Total timeout of the serial interface in ms.</param>
		/// <param name="interCharTimeOut">Inter-character timeout of the serial interface in ms.</param>
		/// <returns>Speed of the serial interface.</returns>
		// Token: 0x0600177A RID: 6010 RVA: 0x00096844 File Offset: 0x00094A44
		public int GetSerialParam(out int dataBits, out string flowControl, out string parity, out int stopBits, out int totalTimeOut, out int interCharTimeOut)
		{
			IntPtr proc = HalconAPI.PreCall(310);
			base.Store(proc, 0);
			HalconAPI.InitOCT(proc, 0);
			HalconAPI.InitOCT(proc, 1);
			HalconAPI.InitOCT(proc, 2);
			HalconAPI.InitOCT(proc, 3);
			HalconAPI.InitOCT(proc, 4);
			HalconAPI.InitOCT(proc, 5);
			HalconAPI.InitOCT(proc, 6);
			int num = HalconAPI.CallProcedure(proc);
			int result;
			num = HalconAPI.LoadI(proc, 0, num, out result);
			num = HalconAPI.LoadI(proc, 1, num, out dataBits);
			num = HalconAPI.LoadS(proc, 2, num, out flowControl);
			num = HalconAPI.LoadS(proc, 3, num, out parity);
			num = HalconAPI.LoadI(proc, 4, num, out stopBits);
			num = HalconAPI.LoadI(proc, 5, num, out totalTimeOut);
			num = HalconAPI.LoadI(proc, 6, num, out interCharTimeOut);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Set the parameters of a serial device.
		///   Instance represents: Serial interface handle.
		/// </summary>
		/// <param name="baudRate">Speed of the serial interface. Default: "unchanged"</param>
		/// <param name="dataBits">Number of data bits of the serial interface. Default: "unchanged"</param>
		/// <param name="flowControl">Type of flow control of the serial interface. Default: "unchanged"</param>
		/// <param name="parity">Parity of the serial interface. Default: "unchanged"</param>
		/// <param name="stopBits">Number of stop bits of the serial interface. Default: "unchanged"</param>
		/// <param name="totalTimeOut">Total timeout of the serial interface in ms. Default: "unchanged"</param>
		/// <param name="interCharTimeOut">Inter-character timeout of the serial interface in ms. Default: "unchanged"</param>
		// Token: 0x0600177B RID: 6011 RVA: 0x000968FC File Offset: 0x00094AFC
		public void SetSerialParam(HTuple baudRate, HTuple dataBits, string flowControl, string parity, HTuple stopBits, HTuple totalTimeOut, HTuple interCharTimeOut)
		{
			IntPtr proc = HalconAPI.PreCall(311);
			base.Store(proc, 0);
			HalconAPI.Store(proc, 1, baudRate);
			HalconAPI.Store(proc, 2, dataBits);
			HalconAPI.StoreS(proc, 3, flowControl);
			HalconAPI.StoreS(proc, 4, parity);
			HalconAPI.Store(proc, 5, stopBits);
			HalconAPI.Store(proc, 6, totalTimeOut);
			HalconAPI.Store(proc, 7, interCharTimeOut);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(baudRate);
			HalconAPI.UnpinTuple(dataBits);
			HalconAPI.UnpinTuple(stopBits);
			HalconAPI.UnpinTuple(totalTimeOut);
			HalconAPI.UnpinTuple(interCharTimeOut);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Set the parameters of a serial device.
		///   Instance represents: Serial interface handle.
		/// </summary>
		/// <param name="baudRate">Speed of the serial interface. Default: "unchanged"</param>
		/// <param name="dataBits">Number of data bits of the serial interface. Default: "unchanged"</param>
		/// <param name="flowControl">Type of flow control of the serial interface. Default: "unchanged"</param>
		/// <param name="parity">Parity of the serial interface. Default: "unchanged"</param>
		/// <param name="stopBits">Number of stop bits of the serial interface. Default: "unchanged"</param>
		/// <param name="totalTimeOut">Total timeout of the serial interface in ms. Default: "unchanged"</param>
		/// <param name="interCharTimeOut">Inter-character timeout of the serial interface in ms. Default: "unchanged"</param>
		// Token: 0x0600177C RID: 6012 RVA: 0x00096990 File Offset: 0x00094B90
		public void SetSerialParam(int baudRate, int dataBits, string flowControl, string parity, int stopBits, int totalTimeOut, int interCharTimeOut)
		{
			IntPtr proc = HalconAPI.PreCall(311);
			base.Store(proc, 0);
			HalconAPI.StoreI(proc, 1, baudRate);
			HalconAPI.StoreI(proc, 2, dataBits);
			HalconAPI.StoreS(proc, 3, flowControl);
			HalconAPI.StoreS(proc, 4, parity);
			HalconAPI.StoreI(proc, 5, stopBits);
			HalconAPI.StoreI(proc, 6, totalTimeOut);
			HalconAPI.StoreI(proc, 7, interCharTimeOut);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Close a serial device.
		///   Instance represents: Serial interface handle.
		/// </summary>
		// Token: 0x0600177D RID: 6013 RVA: 0x00096A00 File Offset: 0x00094C00
		public void CloseSerial()
		{
			IntPtr proc = HalconAPI.PreCall(313);
			base.Store(proc, 0);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Open a serial device.
		///   Modified instance represents: Serial interface handle.
		/// </summary>
		/// <param name="portName">Name of the serial port. Default: "COM1"</param>
		// Token: 0x0600177E RID: 6014 RVA: 0x00096A34 File Offset: 0x00094C34
		public void OpenSerial(string portName)
		{
			this.Dispose();
			IntPtr proc = HalconAPI.PreCall(314);
			HalconAPI.StoreS(proc, 0, portName);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
		}
	}
}
